import random
import string
import model

from google.appengine.api import users

def add_user( url, user_dict ):
    user = users.get_current_user()
    if user:
        user_dict['user'] = user
        user_dict['user_auth_url'] = users.create_logout_url( url )
        user_dict['paypal_email'] = paypal_email( user )
    else:
        user_dict['user_auth_url'] = users.create_login_url( url )

def paypal_email( user ):
    profile = model.Profile.from_user( user )
    if profile == None:
        return user.email() # no profile
    else:
        return profile.paypal_email

def random_alnum( count ):
    chars = string.letters + string.digits
    result = ''
    for i in range(count):
        random.seed(i)
        result += random.choice(chars)
    return result
